package com.ruoyi.verify.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.system.feign.RemoteMenuService;
import com.ruoyi.verify.domain.VerifyQuestionnaire;
import com.ruoyi.verify.service.IVerifyPlanInfoService;
import com.ruoyi.verify.service.IVerifyTemplateContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.verify.domain.VerifyReport;
import com.ruoyi.verify.service.IVerifyReportService;
import com.ruoyi.verify.utils.MyUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * 审核结果 提供者
 *
 * @author ruoyi
 * @date 2020-10-24
 */
@RestController
@RequestMapping("report")
public class VerifyReportController extends MyUtils
{

	@Autowired
	private IVerifyReportService verifyReportService;

	@Autowired
	private RemoteMenuService sysMenuClient;

	@Autowired
	private IVerifyPlanInfoService verifyPlanInfoService;

	@Autowired
	private IVerifyTemplateContentService verifyTemplateContentService;

	/**
	 * 查询${tableComment}
	 */
	@GetMapping("get/{reportid}")
	public VerifyReport get(@PathVariable("reportid") Long reportid)
	{
		return verifyReportService.selectVerifyReportById(reportid);

	}

	/**
	 * 点击查询人员问题
	 */
	@GetMapping("getproblem/{planid}")
	public R get(@PathVariable("planid") String planid)
	{
		return resultObject(verifyReportService.selectProblemListByClick(planid));
	}

	/**
	 * 查询审核结果列表
	 */
	@GetMapping("list")
	public R list(VerifyReport verifyReport)
	{
		startPage();
        return result(verifyReportService.selectVerifyReportList(verifyReport));
	}


	/**
	 * 新增保存审核结果
	 */
	@PostMapping("save")
	public R addSave(@RequestBody VerifyReport verifyReport)
	{
		return toAjax(verifyReportService.insertVerifyReport(verifyReport));
	}

	/**
	 * 发布审核结果
	 */
	@PostMapping("releaseReport")
	public R releaseReport(@RequestBody Map<String, Object> datas)
	{
//		addProblem(datas);

		Long reportid = Long.valueOf(String.valueOf(datas.get("reportid")));
		if(datas.get("fileid")==null){
			return R.error(505,"发布前请先将打印签字后的问题清单上传！");
		}
		VerifyReport verifyReport = verifyReportService.selectVerifyReportById(reportid);

		return toAjax(verifyReportService.releaseReport(verifyReport.getPlanid(),verifyReport.getReportid(),2l,verifyReport.getVendorname()));
	}

	/**
	 * 发布审核结果
	 */
	@PostMapping("releaseVdaReport")
	public R releaseVdaReport(@RequestBody Map<String, Object> datas)
	{
//		addProblem(datas);

		Long reportid = Long.valueOf(String.valueOf(datas.get("reportid")));

		VerifyReport verifyReport = verifyReportService.selectVerifyReportById(reportid);

		return toAjax(verifyReportService.releaseReport(verifyReport.getPlanid(),verifyReport.getReportid(),2l,verifyReport.getVendorname()));
	}



    /**
     * 新增审核问题
     * @param
     * @return
     */
	@PostMapping("saveProblem")
    public Map<String, String> addProblem(@RequestBody Map<String, Object> datas)
    {
		return jacResultMsg(verifyReportService.insertVerifyProblem(datas));
    }

	/**
	 * 修改保存审核结果
	 */
	@PostMapping("update")
	public R editSave(@RequestBody VerifyReport verifyReport)
	{
		return toAjax(verifyReportService.updateVerifyReport(verifyReport));
	}
	/**
	 * 修改保存VDA审核结果
	 */
	@PostMapping("updateVda")
	public R editVdaSave(@RequestBody VerifyReport verifyReport)
	{
		return toAjax(verifyReportService.updateVdaVerifyReport(verifyReport));
	}

	/**
	 * 修改保存审核 问题结果
	 */
	@PostMapping("updateProblem")
	public Map<String, String> updateProblem(@RequestBody Map<String, List<HashMap<String, Object>>> datas)
	{
		return jacResultMsg(verifyReportService.updateProblem(datas));
	}

	/**
	 * 查询问题列表
	 */
	@GetMapping("problemlist")
	public R problemlist(String planid)
	{
		startPage();
		boolean ifteamleader = ifhaveauthority(Long.parseLong(planid));
		return result(verifyReportService.selectProblemListById(planid,ifteamleader));
	}



	/**
	 * 删除${tableComment}
	 */
	@PostMapping("remove")
	public R remove(String ids)
	{
		return toAjax(verifyReportService.deleteVerifyReportByIds(ids));
	}

	/**
	 * 删除审核问题信息
	 */
	@PostMapping("removeProblem")
	public R removeProblem(Long problemid)
	{
		return toAjax(verifyReportService.deleteProblemById(problemid));
	}



	public boolean ifhaveauthority(Long planid){

		HttpServletRequest request = ServletUtils.getRequest();
		String tmpUserKey = request.getHeader(Constants.CURRENT_ID);

		if (Optional.ofNullable(tmpUserKey).isPresent())
		{
			Long userId = Long.valueOf(tmpUserKey);
			if (userId == 1L || verifyPlanInfoService.isgroupleader(userId,planid))
			{
				return true;
			}

			String s1 = verifyPlanInfoService.selectPermsByUserId(userId).stream().filter(s -> s!=null).filter(s -> s.equals("auditing:verifyplan:manager")).findAny().orElse(null);
			return s1!=null?true:false;
		}
		return false;
	}

}
